const router = require('koa-router')()
// 路由前缀
router.prefix('/stu')
// 引入学生模型
const stuModel = require('../models/stu')
router.get('/',async function (ctx,next){
  // ctx.body = 'this is stu list page'
  // 获取数据
  var data = await stuModel.mysql('select * from student')
  await ctx.render('index',{
    stus:data
  })
})

router.get('/create', function (ctx, next) {
  ctx.render('post')
  // ctx.body = 'this is stu create page'
})

router.get('/docreate', async function (ctx, next) {
  let getData = ctx.query
  // console.log('ress',getData)
  // ress [Object: null prototype] {
  //   name: 'ccc',
  //       pwd: '123',
  //       age: '12',
  //       sex: '男'
  // }

  let data = await stuModel.mysql(`insert into student values(null,'${getData.name}',${getData.age}, '${getData.pwd}','${getData.sex}', null)`)
  // 判断跳转
  console.log(data)
  ctx.response.redirect('/stu')
})
module.exports = router
